package com.learn.project2.service.admin.region;

import com.github.pagehelper.PageHelper;
import com.learn.project2.bean.MarketRegion;
import com.learn.project2.bean.MarketRegionExample;
import com.learn.project2.mapper.MarketRegionMapper;
import com.learn.project2.model.vo.admin.region.RegionListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO 商场管理-行政区域
 * @author 亓永杰
 * @date 2022-01-06 22:24
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = {"marketRegion"})
public class RegionServiceImpl implements RegionService {

    @Autowired
    MarketRegionMapper marketRegionMapper;


    @Override
    @Cacheable
    public RegionListVO listRegions() {


        //设置页码和页面大小
        PageHelper.startPage(1,31);
        RegionListVO regionListVO = new RegionListVO();
        List<RegionListVO.ListBean> provinceVOS = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            MarketRegion proince = marketRegionMapper.selectByPrimaryKey(i);
            RegionListVO.ListBean provinceVO = new RegionListVO.ListBean();
            provinceVO.setId(proince.getId());
            provinceVO.setName(proince.getName());
            provinceVO.setCode(proince.getCode());
            provinceVO.setType(proince.getType());

            MarketRegionExample cityExample = new MarketRegionExample();
            cityExample.createCriteria().andPidEqualTo(proince.getId());
            List<MarketRegion> cities = marketRegionMapper.selectByExample(cityExample);
            List<RegionListVO.ListBean.ChildrenBean> cityVOS = new ArrayList<>();
            for (MarketRegion city : cities) {
                RegionListVO.ListBean.ChildrenBean cityVo = new RegionListVO.ListBean.ChildrenBean();
                cityVo.setId(city.getId());
                cityVo.setCode(city.getCode());
                cityVo.setType(city.getType());
                cityVo.setName(city.getName());

                MarketRegionExample areaExample = new MarketRegionExample();
                areaExample.createCriteria().andPidEqualTo(city.getId());
                List<MarketRegion> areas = marketRegionMapper.selectByExample(areaExample);
                List<RegionListVO.ListBean.ChildrenBean.ChildrenBean2> areaVOS = new ArrayList<>();
                for (MarketRegion area : areas) {

                    RegionListVO.ListBean.ChildrenBean.ChildrenBean2 areaVo = new RegionListVO.ListBean.ChildrenBean.ChildrenBean2();
                    areaVo.setId(area.getId());
                    areaVo.setCode(area.getCode());
                    areaVo.setName(area.getName());
                    areaVo.setType(area.getType());
                    areaVOS.add(areaVo);
                }
                cityVo.setChildren(areaVOS);
                cityVOS.add(cityVo);

            }
            provinceVO.setChildren(cityVOS);
            provinceVOS.add(provinceVO);
        }
        regionListVO.setLimit(31);
        regionListVO.setPage(1);
        regionListVO.setList(provinceVOS);
        regionListVO.setPages(1);
        regionListVO.setTotal(31);

        return regionListVO;
    }
}
